﻿using System;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using TongTongMall.Runtime.Security;
using TongTongMall.Runtime.Session;

namespace TongTongMall.MallWeb.Application.Configuration.Session
{
    public class MallWebSession : TongTongMallSession, IMallWebSession
    {
        /// <summary>
        /// 授权平台
        /// </summary>
        public string Platform
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }

                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return null;
                }

                var platformClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == OAuthClaimTypes.Platform);
                if (platformClaim == null || string.IsNullOrEmpty(platformClaim.Value))
                {
                    return null;
                }
                return platformClaim.Value;
            }
        }

        /// <summary>
        /// 匿名用户信息
        /// </summary>
        public virtual string SessionKey
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }

                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return null;
                }

                var sessionKeyClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == OAuthClaimTypes.SessionKey);
                if (sessionKeyClaim == null || string.IsNullOrEmpty(sessionKeyClaim.Value))
                {
                    return null;
                }
                return sessionKeyClaim.Value;
            }
        }

        /// <summary>
        /// 授权用户版本号
        /// 发生变化后需要重新登录
        /// </summary>
        public int TicketVersion
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return default(int);
                }

                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return default(int);
                }

                var tickVersionClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == OAuthClaimTypes.TicketVersion);
                if (tickVersionClaim == null || string.IsNullOrEmpty(tickVersionClaim.Value))
                {
                    return default(int);
                }
                int version;
                if (!int.TryParse(tickVersionClaim.Value, out version))
                {
                    return default(int);
                }
                return version;
            }
        }
    }
}